package com.xian.gaoxiao.ui.activity;

import java.util.ArrayList;

import com.xian.gaoxiao.MainApplication;
import com.xian.gaoxiao.R;
import com.xian.gaoxiao.settings.MainSettings;
import com.xian.gaoxiao.utils.Utilities;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FirstCartoonAc extends Activity {

	private static final int PAGE_SIZE = 6;

	private int currentPage = 0;

	private Context mContext;
	private RelativeLayout lodingLayout, cartoonLayout;

	private Button btnBegin;
	private View page;
	private ArrayList<View> pageViews;
	private ImageView imageView;
	private ImageView[] imageViews = new ImageView[PAGE_SIZE];
	private ViewPager viewPager;
	private LinearLayout pageindicator;
	private MainApplication mApp;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			lodingLayout.setVisibility(View.GONE);
			cartoonLayout.setVisibility(View.VISIBLE);
			super.handleMessage(msg);
		}

	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.ac_help);
		lodingLayout = (RelativeLayout) findViewById(R.id.cartoon_loding);
		cartoonLayout = (RelativeLayout) findViewById(R.id.cartoon_layout);
		mApp = (MainApplication) this.getApplication();

		if (mApp.getScreenWidth() != 0) {
			gotoMainAc();
		} else {
			lodingLayout.post(new Runnable() {
				public void run() {
					calStatusBarHeight();
				}
			});
			new Thread(new InitCartoon()).start();
			calScreenSize();

		}
	}

	class InitCartoon implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			LayoutInflater inflater = getLayoutInflater();
			pageViews = new ArrayList<View>(PAGE_SIZE);
			imageViews = new ImageView[PAGE_SIZE];
			viewPager = (ViewPager) findViewById(R.id.help_content_vp);
			pageindicator = (LinearLayout) findViewById(R.id.help_ll_pageindicator);

			for (int i = 0, res = R.drawable.help_1; i < PAGE_SIZE; i++, res++) {
				imageView = new ImageView(mContext);
				page = new View(mContext);
				imageView.setLayoutParams(new LayoutParams(30, 30));
				imageView.setPadding(30, 30, 30, 30);
				imageViews[i] = imageView;
				imageViews[i]
						.setBackgroundResource(R.drawable.pageindicator_off);

				if (i == PAGE_SIZE - 1) {
					page = inflater.inflate(R.layout.item_help_viewpagerlast,
							null);
					btnBegin = (Button) page.findViewById(R.id.help_content_bt);
					btnBegin.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							gotoMainAc();
						}
					});

				} else {
					page = inflater.inflate(R.layout.item_help_viewpager, null);
				}
				((ImageView) page.findViewById(R.id.help_content_vp_iv))
						.setBackgroundResource(res);
				pageViews.add(page);
				pageindicator.addView(imageViews[i]);
			}
			imageViews[currentPage]
					.setBackgroundResource(R.drawable.pageindicator_on);
			viewPager.setAdapter(new GuidePageAdapter());
			viewPager.setOnPageChangeListener(new GuidePageChangeListener());
			handler.sendEmptyMessage(0);
		}

	}

	class GuidePageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return PAGE_SIZE;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub
		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub

		}
	}

	class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onPageSelected(int arg0) {

			imageViews[currentPage]
					.setBackgroundResource(R.drawable.pageindicator_off);
			imageViews[arg0].setBackgroundResource(R.drawable.pageindicator_on);
			currentPage = arg0;

		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			gotoMainAc();
		}

		return true;
	}

	private void gotoMainAc() {
		Intent intent = new Intent(this, MainAc.class);
		startActivity(intent);
		this.finish();
	}

	private void calScreenSize() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels; // 得到宽度
		int height = dm.heightPixels; // 得到高度
		mApp.setScreenHeight(height);
		mApp.setScreenWidth(width);
		SharedPreferences pre = this.getSharedPreferences(
				MainSettings.PREFERENCE_FILENAME, Context.MODE_PRIVATE);
		Editor editor = pre.edit();
		editor.putInt(MainSettings.SCREEN_WIDTH, width);
		editor.putInt(MainSettings.SCREEN_HEIGHT, height);
		editor.commit();
	}

	/**
	 * get statusbar height
	 */
	private void calStatusBarHeight() {
		Rect rect = new Rect();
		Window win = this.getWindow();
		win.getDecorView().getWindowVisibleDisplayFrame(rect);
		int statusBarHeight = rect.top;
		mApp.setStatusBarHeight(statusBarHeight);
		SharedPreferences pre = this.getSharedPreferences(
				MainSettings.PREFERENCE_FILENAME, Context.MODE_PRIVATE);
		Editor editor = pre.edit();
		editor.putInt(MainSettings.STATUS_BAR_HEIGHT, statusBarHeight);
		editor.commit();
		Utilities.print(statusBarHeight + ":" + win.getDecorView().getHeight());
	}

}